package com.cdvtc.fengchao.Servlet;

import com.cdvtc.fengchao.dao.GeneralDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet("/admin/orderManagementServlet")
public class OrderManagementServlet extends HttpServlet {

    private GeneralDao generalDao = new GeneralDao();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 查询订单数据
        List<Map<String, Object>> orders = generalDao.getOrders();
        System.out.println("查询到的订单数量: " + orders.size());
        request.setAttribute("orders", orders);

        // 转发到 JSP 页面
        request.getRequestDispatcher("/admin/orders.jsp").forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String action = request.getParameter("action");
        if ("add".equals(action)) {
            // 添加订单
            int userId = Integer.parseInt(request.getParameter("user_id"));
            String purchaseTime = request.getParameter("purchase_time");
            String paymentStatus = request.getParameter("payment_status");
            String paymentMethod = request.getParameter("payment_method");
            double totalAmount = Double.parseDouble(request.getParameter("total_amount"));
            int result = generalDao.addOrder(userId, purchaseTime, paymentStatus, paymentMethod, totalAmount);
            if (result > 0) {
                System.out.println("订单添加成功");
            } else {
                System.out.println("订单添加失败");
            }
        } else if ("update".equals(action)) {
            // 更新订单
            int id = Integer.parseInt(request.getParameter("id"));
            int userId = Integer.parseInt(request.getParameter("user_id"));
            String purchaseTime = request.getParameter("purchase_time");
            String paymentStatus = request.getParameter("payment_status");
            String paymentMethod = request.getParameter("payment_method");
            double totalAmount = Double.parseDouble(request.getParameter("total_amount"));
            int result = generalDao.updateOrder(id, userId, purchaseTime, paymentStatus, paymentMethod, totalAmount);
            if (result > 0) {
                System.out.println("订单更新成功");
            } else {
                System.out.println("订单更新失败");
            }
        } else if ("delete".equals(action)) {
            // 删除订单
            int id = Integer.parseInt(request.getParameter("id"));
            int result = generalDao.deleteOrder(id);
            if (result > 0) {
                System.out.println("订单删除成功");
            } else {
                System.out.println("订单删除失败");
            }
        }
        response.sendRedirect(request.getContextPath() + "/admin/orderManagementServlet");
    }
}